package com.bbms.dao.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bbms.dao.UserDao;
import com.bbms.entity.Page;
import com.bbms.entity.User;
import com.bbms.util.HibernateUtil;

/**
 * 用户DAO实现类
 * @author Administrator
 */
@Repository("UserDaoImpl")
public class UserDaoImpl implements UserDao{

	/*
	 * 添加用户
	 * @see com.bbms.dao.BaseDAO#save(java.lang.Object)
	 */
	@Override
	public void save(User t) {
		HibernateUtil.save(t);
	}

	/*
	 * 修改用户
	 * @see com.bbms.dao.BaseDAO#update(java.lang.Object)
	 */
	@Override
	public void update(User t) {
		HibernateUtil.update(t);
	}

	/*
	 * 删除用户
	 * @see com.bbms.dao.BaseDAO#delete(java.lang.Object)
	 */
	@Override
	public void delete(User t) {
		HibernateUtil.delete(t);
	}

	/*
	 * 获取分页数据(无查询)
	 * @see com.bbms.dao.BaseDAO#getPage(int, int)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize) {
		return null;
	}

	/*
	 * 获取全部信息（无分页）
	 * @see com.bbms.dao.BaseDAO#query()
	 */
	@Override
	public List<?> query() {
		return null;
	}

	/*
	 * 根据账号获取信息（无分页）
	 * @see com.bbms.dao.BaseDAO#query()
	 */
	@Override
	public List<?> queryByAccount(String account) {
		
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT * FROM bbms_user_info WHERE account LIKE '%"+account+"%'");
		List<?> list=HibernateUtil.executeSqlQuery(sql.toString());
		return list;
	}

	/*
	 * 根据Id修改用户
	 * @see com.bbms.dao.UserDao#updateById(com.bbms.entity.User, int)
	 */
	@Override
	public void updateById(User user, int id) {
		
		StringBuffer sql=new StringBuffer();
		DateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		
		Date birthday=user.getBirthday();
		String date=format.format(birthday);
		
		System.out.println("时间：  "+date);
		
		try{
			
			sql.append( "UPDATE bbms_user_info U "
					  + "SET U.`name`='"+ user.getName() + "' ,U.account='"+ user.getAccount() +"' ,"
					  + "U.`password`='"+ user.getPassword() +"' ,U.IDCard='"+ user.getIDCard() +"' ,"
					  + "U.birthday ='"+ date +"' ,U.phone='"+ user.getPhone() +"' "
					  + "WHERE U.id="+ id +" ");
		
		System.out.println("更改user的sql:  "+sql.toString());
		
		HibernateUtil.executeSqlUpdate(sql.toString());
		}catch(Exception e){
			e.printStackTrace();
		}
		
		
	}

	/* 用户登录
	 * @see com.bbms.dao.UserDao#login(java.lang.String, java.lang.String)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<?, ?> login(String account, String password) {
		
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT COUNT(u.id) AS login, "
				+ "CASE (m.category IS NULL) WHEN 1 THEN 0 ELSE m.category END AS authority "
				+ "FROM bbms_user_info AS u LEFT JOIN bbms_user_reader AS r ON (r.user_info_id = u.id) "
				+ "LEFT JOIN bbms_user_manager AS m ON (m.user_info_id = u.id) "
				+ "WHERE u.account = '" + account + "' AND u.`password` = '" + password + "'");
		List<HashMap> list = (List<HashMap>) HibernateUtil.executeSqlQuery(sb.toString());
		Map message = list.get(0);
		
		return message;
	}

	/* 修改个人信息
	 * @see com.bbms.dao.UserDao#updateUser(com.bbms.entity.User)
	 */
	@Override
	public void updateUser(User user) {
		
		StringBuffer sb = new StringBuffer();
		sb.append("UPDATE bbms_user_info AS u "
					+ "SET u.`name` = '" + user.getName() + "', "
					+ "u.`password` = '" + user.getPassword() + "', "
					+ "u.phone = '" + user.getPhone() + "', "
					+ "u.birthday = " + (user.getBirthday() != null ? "'" + user.getBirthday() + "'" : "NULL")  + " "
					+ "WHERE u.account = '" + user.getAccount() + "'");
		
		HibernateUtil.executeSqlUpdate(sb.toString());
	}
	
}
